package cn.edu.zut.Business;

import cn.edu.zut.Dao.StudentDao;
import cn.edu.zut.Model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ScoreService {


    StudentDao studentDao;
    @Autowired
    public void setStudentDao(StudentDao studentDao)
    {
        this.studentDao=studentDao;
    }
    public void insertStudent(Student student)
    {
        studentDao.insertStudent(student);
    }
    public float averageScore()
    {
        float sum=0;
        List<Student> students=studentDao.queryAll();
        for(Student s:students)
        {
            sum +=s.getScore();
        }
        return sum/students.size();
    }
    @Transactional
    public void modifyScore()
    {
        List<Student> students=studentDao.queryAll();
        for(Student s: students)
        {
            int score=(int)s.getScore();
            int G=score/10;
            switch(G)
            {
                case 10:s.setScore(1f);s.setDj("S");studentDao.modifyStudent(s);break;
                case 9:s.setScore(1f);s.setDj("A");studentDao.modifyStudent(s);break;
                case 8:s.setScore(2f);s.setDj("B");studentDao.modifyStudent(s);break;
                case 7:s.setScore(3f);s.setDj("C");studentDao.modifyStudent(s);break;
                case 6:s.setScore(4f);s.setDj("D");studentDao.modifyStudent(s);break;
                default:s.setScore(5f);s.setDj("E");studentDao.modifyStudent(s);
            }
            System.out.println(s.getScore());
        }
    }
}
